﻿#include "CFDPostClipSliceWidgetSphere.h"
#include "ui_CFDPostClipSliceWidgetSphere.h"

#include "GUIFrame/MainWindow.h"
#include "PostGraphAdaptor/PostGraphWidgetSphere.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKVec3D.h"
#include "FITK_Kernel/FITKCore/FITKVec3DAlg.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostClipSphere.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostSliceSphere.h"

namespace GUI {
    CFDPostClipSliceWidgetSphere::CFDPostClipSliceWidgetSphere(ClipSlice widgetType, int currentDataID, int parentDataID, QWidget* parent) :
        CFDPostClipSliceWidgetBase(widgetType, parent), _parentDataID(parentDataID) {
        _currentDataID = currentDataID;

        _ui = new Ui::CFDPostClipSliceWidgetSphere();
        _ui->setupUi(this);

        MainWindow* mainWin = FITKAPP->getGlobalData()->getMainWindowT<MainWindow>();
        if (mainWin == nullptr) {
            return;
        }
        QWidget* currentWidget = (QWidget*)this->getGraphWidget(); //mainWin->getRenderWidget()->getCurrentWidget();
        _sphereWidget = new Interface::PostGraphWidgetSphere(currentWidget);

        connect(_sphereWidget, SIGNAL(sigValueChange(double*, double)), this, SLOT(slotVTKWidgetValueChange(double*, double)));
        connect(_ui->lineEdit_CenterX, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_CenterY, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_CenterZ, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_Radius, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
    }

    CFDPostClipSliceWidgetSphere::~CFDPostClipSliceWidgetSphere() {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }

        if (_sphereWidget) {
            delete _sphereWidget;
            _sphereWidget = nullptr;
        }
    }

    void CFDPostClipSliceWidgetSphere::getDataFromWidget(Interface::FITKAbstractCFDPostData * postData) {
        double origin[3] = { 0, 0, 0 };
        double radius = 0;
        origin[0] = _ui->lineEdit_CenterX->value();
        origin[1] = _ui->lineEdit_CenterY->value();
        origin[2] = _ui->lineEdit_CenterZ->value();
        radius = _ui->lineEdit_Radius->value();

        switch (_widgetType) {
            case GUI::ClipSlice::None:
                break;
            case GUI::ClipSlice::Clip: {
                Interface::FITKCFDPostClipSphere* currentData = dynamic_cast<Interface::FITKCFDPostClipSphere*>(postData);
                if (currentData == nullptr) {
                    return;
                }
                currentData->setValue(origin, radius);
                break;
            }
            case GUI::ClipSlice::Slice: {
                Interface::FITKCFDPostSliceSphere* currentData = dynamic_cast<Interface::FITKCFDPostSliceSphere*>(postData);
                if (currentData == nullptr) {
                    return;
                }
                currentData->setValue(origin, radius);
                break;
            }
        }
    }

    void CFDPostClipSliceWidgetSphere::setDataToWidget(Interface::FITKAbstractCFDPostData* postParentData, Interface::FITKAbstractCFDPostData* postData) {
        //获取边界、中心点、轴向
        double bounds[6] = { 0, 0, 0, 0, 0, 0 };
        double origin[3] = { 0, 0, 0 };
        double radius = 0;

        if (postParentData) {
            postParentData->getOrigin(origin);
            postParentData->getBounds(bounds);
            Core::FITKPoint point1(bounds[0], bounds[2], bounds[4]);
            Core::FITKPoint point2(bounds[1], bounds[3], bounds[5]);
            radius = Core::Distance(point1, point2) / 2.0;
        }

        switch (_widgetType) {
            case GUI::ClipSlice::None:
                return;
            case GUI::ClipSlice::Clip: {
                Interface::FITKCFDPostClipSphere* currentData = dynamic_cast<Interface::FITKCFDPostClipSphere*>(postData);
                if (currentData) {
                    currentData->getValue(origin, radius);
                }
                break;
            }
            case GUI::ClipSlice::Slice: {
                Interface::FITKCFDPostSliceSphere* currentData = dynamic_cast<Interface::FITKCFDPostSliceSphere*>(postData);
                if (currentData) {
                    currentData->getValue(origin, radius);
                }
                break;
            }
        }

        _ui->lineEdit_CenterX->setText(QString::number(origin[0]));
        _ui->lineEdit_CenterY->setText(QString::number(origin[1]));
        _ui->lineEdit_CenterZ->setText(QString::number(origin[2]));
        _ui->lineEdit_Radius->setText(QString::number(radius));

        _sphereWidget->setValue(origin, radius);
    }

    void CFDPostClipSliceWidgetSphere::showEvent(QShowEvent * event) {
        if (_sphereWidget) {
            _sphereWidget->setIsShow(true);
            reRender();
        }
        CFDPostClipSliceWidgetBase::showEvent(event);
    }

    void CFDPostClipSliceWidgetSphere::closeEvent(QCloseEvent * event) {
        if (_sphereWidget) {
            _sphereWidget->setIsShow(false);
            delete _sphereWidget;
            _sphereWidget = nullptr;
            reRender();
        }
        CFDPostClipSliceWidgetBase::closeEvent(event);
    }

    void CFDPostClipSliceWidgetSphere::hideEvent(QHideEvent * event) {
        if (_sphereWidget) {
            _sphereWidget->setIsShow(false);
            delete _sphereWidget;
            _sphereWidget = nullptr;
            reRender();
        }
        CFDPostClipSliceWidgetBase::hideEvent(event);
    }

    void CFDPostClipSliceWidgetSphere::slotVTKWidgetValueChange(double* center, double radius) {
        if (_ui == nullptr) {
            return;
        }
        _ui->lineEdit_CenterX->setText(QString::number(center[0]));
        _ui->lineEdit_CenterY->setText(QString::number(center[1]));
        _ui->lineEdit_CenterZ->setText(QString::number(center[2]));
        _ui->lineEdit_Radius->setText(QString::number(radius));
    }

    void CFDPostClipSliceWidgetSphere::slotDataChange() {
        if (_sphereWidget == nullptr) {
            return;
        }
        double origin[3] = { 0, 0, 0 };
        double radius = 0.0;
        origin[0] = _ui->lineEdit_CenterX->value();
        origin[1] = _ui->lineEdit_CenterY->value();
        origin[2] = _ui->lineEdit_CenterZ->value();
        radius = _ui->lineEdit_Radius->value();
        _sphereWidget->setValue(origin, radius);
        reRender();
    }
}

